package yxy.game.pm2.command.union.contrib;

import yxy.cherry.data.DataCenter;
import yxy.cherry.data.bean.unioncontribution;
import yxy.cherry.data.string.GameString;
import yxy.cherry.game.base.message.MessageEmitterHandler;
import yxy.cherry.game.base.message.annotation.MessageSource;
import yxy.cherry.game.base.message.annotation.MessageType;
import yxy.game.pm2.Result;
import yxy.game.pm2.bean.Currency;
import yxy.game.pm2.bean.PlayerBean;
import yxy.game.pm2.bean.currency.CurrencyNotEnoughException;
import yxy.game.pm2.bean.module.RedisLock;
import yxy.game.pm2.bean.mq.CCReason;
import yxy.game.pm2.bean.task.TaskType;
import yxy.game.pm2.command.Command;
import yxy.game.pm2.message.UnionMessage;
import yxy.game.pm2.module.CurrencyHandle;
import yxy.game.pm2.module.task.TaskHandle;
import yxy.game.pm2.module.union.UnionBean;
import yxy.game.pm2.module.union.UnionContribution;
import yxy.game.pm2.module.union.UnionContribution.ContributeType;
import yxy.game.pm2.module.union.UnionHandle;
import yxy.game.pm2.module.union.UnionHandle.UnionPlayerEntry;
import yxy.game.pm2.module.union.UnionLog;
import yxy.game.pm2.module.union.player.UnionPlayer;
import yxy.game.pm2.module.union.player.UnionPlayerContribution;

/** 联盟捐献 联盟捐献 */
@MessageType(proto = Command.UnionContribute, classtype = UnionMessage.UnionContributeRequest.class)
public class CMD_UnionContribute extends Command<UnionMessage.UnionContributeRequest> {

	@Override
	public void execute(MessageSource messageSource, UnionMessage.UnionContributeRequest request) {
		String playerCode = messageSource.getTag();
		UnionMessage.ContributeType type = request.getType();

		UnionMessage.UnionContributeResponse.Builder response = UnionMessage.UnionContributeResponse.newBuilder();

		// 检查是否已经有联盟
		UnionPlayerEntry entry = new UnionPlayerEntry(playerCode);
		Result result = new UnionHandle().checkPlayerUnion(entry);
		if (!result.equals(Result.UnionIn)) {
			send(messageSource, response, result);
			return;
		}
		RedisLock uLock = UnionBean.lock(entry.getUnionBean().getCode());
		RedisLock pLock = UnionPlayer.lock(entry.getPlayerCode());
		try {
			if (uLock.tryLock() && pLock.tryLock()) {

				UnionBean union = entry.reloadUnionBean();
				UnionPlayer unionPlayer = entry.reloadUnionPlayer();
				UnionPlayerContribution playerContribution = unionPlayer.getContribution();

				if (!playerContribution.getContribType().equals(ContributeType.NONE)) {
					send(messageSource, response, Result.UnionContribDone);// 已经捐献过
					return;
				}

				UnionContribution contribution = UnionContribution.get(union);

				unioncontribution data = DataCenter.getData(type.getNumber(), unioncontribution.class);
				Currency cost = Currency.create(data.getCosttype(), data.getCostid(), data.getCostnum());
				try {
					new CurrencyHandle().cost(playerCode, CCReason.联盟捐献, cost);
				} catch (CurrencyNotEnoughException e) {
					send(messageSource, response, new Result(Result.CostFail, e.getType().getNumber(), e.getDataId()));// 消费不足
					return;
				}

				Currency reward = Currency.create(data.getRewardtype(), data.getRewardid(), data.getRewardnum());
				int contribute = data.getRewardnum();

				new CurrencyHandle().addCurrency(playerCode, CCReason.联盟捐献, reward);

				// 进度
				contribution.setProgress(contribution.getProgress() + data.getRewardstage());
				contribution.set();
				// 经验经验
				int orilevel = union.getLevel();
				int addexp = data.getRewardexp();
				union.addexp(addexp);
				// 贡献
				playerContribution.setContribType(ContributeType.forNumber(type.getNumber()));
				playerContribution.setContribDaliy(contribute);
				playerContribution.setContribution(playerContribution.getContribution() + contribute);
				unionPlayer.set();

				response.setType(type);
				response.setProgress(contribution.getProgress());
				response.setLevel(union.getLevel());
				response.setExp(union.getExp());

				send(messageSource, response, Result.Success);

				TaskHandle.handle(playerCode, TaskType.统计型_联盟_捐献_次数, 1);

				// 联盟日志
				PlayerBean player = PlayerBean.get(unionPlayer.getPlayerCode());
				UnionLog.add(union.getCode(), GameString.联盟日志捐献(player.getName(), addexp).getText());

				if (union.getLevel() != orilevel) {
					// 等级变化
					new UnionHandle().levelUpdate(union);
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
			send(messageSource, response, Result.DataError);
		} finally {
			uLock.unlock();
			pLock.unlock();
		}
	}

	private void send(MessageSource messageSource, UnionMessage.UnionContributeResponse.Builder response, Result result) {
		response.setResult(result.packResult());
		MessageEmitterHandler.getInstance().send(messageSource, response.build());
	}
}